// 过滤可派单人员
export const filterEmployeess = (
  employee = [],
  date = new Date().getTime()
) => {
  if (!employee.length) return [];
  const dayStart = new Date(date);
  const dayEnd = new Date(date);
  dayStart.setHours(9, 0, 0, 0);
  dayEnd.setHours(18, 0, 0, 0);
  return employee.filter((item) => {
    // 保留包含明天时间段或明天上午/下午的员工
    if (item.depart === 0) return false;

    if (
      item.leaveEnd * 1000 <= dayStart ||
      (item.leaveStart * 1000 <= dayStart && item.leaveEnd * 1000 < dayEnd) ||
      (item.leaveStart * 1000 >= dayStart && item.leaveEnd * 1000 < dayEnd) ||
      item.leaveStart * 1000 > dayEnd ||
      (item.leaveEnd === 0 && item.leaveStart === 0) ||
      (item.leaveEnd === null && item.leaveStart === null)
    )
      return true;

    // 其他情况过滤掉
    return false;
  });
};
/**
 *
 * @param {Array} list 员工列表
 * @param {string} time 'AM' / 'PM' 上午/下午
 * @param {Number,Date} date 派单当天时间 通过xlsx列表获取
 * @param {Array} list1 上午/下午的客户
 * @returns 上午/下午 派单员工列表
 */
export const filterStaffTime = (
  list = [],
  time,
  date = new Date().getTime(),
  list1 = []
) => {
  const startDay = new Date(date);
  const noonDay = new Date(date);
  const endDay = new Date(date);

  startDay.setHours(9, 0, 0, 0);
  noonDay.setHours(12, 0, 0, 0);
  endDay.setHours(18, 0, 0, 0);
  let num = [];

  let type = [{0:0, 2: 2}, {1:1}];


  num = list.filter((item) => {
    // 过滤掉不包含的职责
    if (!list1.some((i) => {

      let res = type[item.post];

      return res[i.type] === i.type;

    })) return false;

    // 过滤不在客户地区范围内的员工
    if (
      !list1.some((i) => i.areaId === item.areaId && i.cityId === item.cityId)
    )
      return false;

    // if (list1.some((i) => i.userId === item.id)) return false; //过滤指派人员

    if (time === "AM") {
      if (
        item.leaveStart === 0 ||
        item.leaveEnd === 0 ||
        item.leaveStart === null ||
        item.leaveEnd == null ||
        item.leaveEnd * 1000 <= startDay ||
        item.leaveStart * 1000 >= noonDay
      )
        return true;

      return false;
    }

    if (time === "PM") {
      if (
        item.newPeople === 1 &&
        item.leaveEnd * 1000 >= startDay &&
        item.leaveEnd * 1000 <= noonDay
      )
        {return true;}
      
     

      if (item.newPeople === 1) return false;

      if (
        item.leaveStart === 0 ||
        item.leaveEnd === 0 ||
        item.leaveStart === null ||
        item.leaveEnd == null ||
        item.leaveEnd * 1000 <= noonDay ||
        item.leaveStart * 1000 > endDay
      )
        return true;

      return false;
    }
  });
  return num;
};
